package com.mintandleaf.webkits.core;

import com.mintandleaf.webkits.data.WebKitsDataModel;
import com.mintandleaf.webkits.validator.WebKitsValidator;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Abstract Web Form processing with custom validation
 * @author teguhh
 */
public abstract class WebKitsWebForm implements WebKitsWebModule{

    protected WebKitsDataModel model;
    protected WebKitsValidator validator;

    /**
     * Validate the data model against the registered validator
     * @param paramWebKitsDataModel
     * @return true on successfull validation
     */
    public boolean validate(WebKitsDataModel paramWebKitsDataModel){
        if(getValidator()!=null)
            return validator.validate(paramWebKitsDataModel);
        else
            return true;
    }

     /**
     * Invoke processing the Http Request
     * @param paramRequest
     * @param paramResponse
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     */
      public final WebKitsModelAndView invoke(HttpServletRequest paramRequest, HttpServletResponse paramResponse) throws ServletException, IOException {
        if(this.model!=null){
            //aha found a data model working by available model
            //get rewuest parameter and populate the model with appropriate
            //data
            Iterator<String> iter=this.model.getPropertyName().iterator();
            while(iter.hasNext()){
                String tempPropertyName=iter.next();
                if(paramRequest.getParameter(tempPropertyName)!=null){
                    WebKitsParameterType paramType=new WebKitsParameterType(this.model.get(tempPropertyName).getClass());
                    this.model.set(tempPropertyName,paramType.convert(paramRequest.getParameter(tempPropertyName)));
                }else
                    this.model.set(tempPropertyName, null);
            }
        }else{
            //model is null then we create new model for it
            //and populate within the request parameter
            this.model=new WebKitsDataModel();
            Enumeration<String> parameterName=paramRequest.getParameterNames();
            while(parameterName.hasMoreElements()){
                String name=parameterName.nextElement();
                this.model.set(name, paramRequest.getParameter(name));
            }
        }
        if(validate(this.model))
            return onOk(this.model);
        else
            return onFail(this.model);
        
    }

     /**
     * Set assosiated data Model
     * @param WebKitsDataModel object
     */
    protected void setModel(WebKitsDataModel paramModel){
        this.model=paramModel;
    }

    /**
     * Get data Model
     * @return WebKitsDataModel object
     */
    protected WebKitsDataModel getModel(){
        return this.model;
    }

    /**
     * Get available validator
     * @return WebKitsValidator object
     */
    public WebKitsValidator getValidator() {
        return validator;
    }

    /**
     * Set available validator
     * @return WebKitsValidator object
     */
    public void setValidator(WebKitsValidator validator) {
        this.validator = validator;
    }

    /**
     * Overide this methods for processing on successfull
     * Validation process
     * @param paramWebKitsDataModel
     **/
    public abstract WebKitsModelAndView onOk(WebKitsDataModel paramWebKitsDataModel);

    /**
     * Overide this methods for processing on unsuccessfull
     * Validation process
     * @param paramWebKitsDataModel
     **/
    public abstract WebKitsModelAndView onFail(WebKitsDataModel paramWebKitsDataModel);
}
